from django.db import models
import datetime
from django.forms import ModelForm, TextInput, CharField, ChoiceField, TimeField, Select, DateField, DateInput, HiddenInput, ModelChoiceField
from django.forms.extras.widgets import SelectDateWidget
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _

class Timetable(models.Model):
    user = models.ForeignKey(User)
    tt_name = models.CharField(max_length=50)
    tt_description = models.CharField(max_length=100)
    tt_start = models.DateField("Start date of the timetable", auto_now_add=True)
    tt_end = models.DateField("End date of the timetable")

    def __unicode__(self):
        return self.tt_name

class Subject(models.Model):
    timetable = models.ForeignKey(Timetable)
    sub_professor = models.CharField(max_length=50)
    sub_name = models.CharField(max_length=50)
    sub_color = models.CharField(max_length=7)

    def __unicode__(self):
        return self.sub_name

class Lesson(models.Model):
    subject = models.ForeignKey(Subject)
    les_location = models.CharField(max_length=50)

    DAY_CHOICES = (
    ('Mo', 'Monday'),
    ('Tu', 'Tuesday'),
    ('We', 'Wednesday'),
    ('Th', 'Thursday'),
    ('Fr', 'Friday'),
    ('Sa', 'Saturday'),
    ('Su', 'Sunday')
    )

    les_day = models.CharField(max_length=2, choices=DAY_CHOICES, default='Monday')
    les_starttime = models.TimeField("Start Datetime of the lesson")
    les_endtime = models.TimeField("End Datetime of the lesson")

    def __unicode__(self):
        return self.subject.sub_name

class Event(models.Model):
    user = models.ForeignKey(User)
    ev_name = models.CharField(max_length=50)
    ev_description = models.CharField(max_length=100)
    ev_starttime = models.TimeField("Starttime of event")
    ev_startdate = models.DateField("Startdate of event")
    ev_endtime = models.TimeField("Endtime of event")
    ev_enddate = models.DateField("Enddate of event")
    ev_location = models.CharField(max_length=100)

    PERIODIC_CHOICES = (
    ('O', 'Once'),
    ('D', 'Daily'),
    ('W', 'Weekly'),
    ('M', 'Monthly'),
    ('Y', 'Yearly')
    )

    ev_periodic = models.CharField(max_length=1, choices=PERIODIC_CHOICES, default='O')
    ev_color = models.CharField(max_length=7)

    def __unicode__(self):
        return self.ev_name

class TimetableForm(ModelForm):
    tt_name = CharField(widget=TextInput(attrs={'class' : 'input-large', 'id' : 'timetable_name', 'placeholder' : _("timetable name...")}), label='')
    tt_description = CharField(widget=TextInput(attrs={'class' : 'input-large', 'id' : 'timetable_description', 'placeholder' : _("timetable description...")}), label='')
    #tt_start = DateField(widget=DateInput(attrs={'class' : 'input-medium', 'id' : 'timetable_start', 'type' : 'date'}), initial=str(datetime.datetime.now().date()), label=_("start date:")) #HTML5 Date Input for Chrome, bad for Firefox
    tt_start = DateField(widget=SelectDateWidget(attrs={'class' : 'input-small', 'id' : 'timetable_start'}), initial=datetime.date.today(), label=_("start date:"))
    tt_end = DateField(widget=SelectDateWidget(attrs={'class' : 'input-small', 'id' : 'timetable_stop'}), initial=datetime.date.today() + datetime.timedelta(days = 170), label= _("end date:"))

    class Meta:
        model = Timetable
        fields = ('tt_name', 'tt_description', 'tt_start', 'tt_end')

class SubjectForm(ModelForm):
    sub_professor = CharField(widget=TextInput(attrs={'class' : 'input-large', 'id' : 'subject_professor', 'placeholder' : _("professor...")}), label='')
    sub_name = CharField(widget=TextInput(attrs={'class' : 'input-large', 'id' : 'subject_name', 'placeholder' : _("subject...")}), label='')
    sub_color = CharField(widget=TextInput(attrs={'class' : 'input-mini', 'id' : 'subject_color', 'type' : 'color', 'value' : '#8e8e8e'}), label='color:')
    timetable = ModelChoiceField(queryset=Timetable.objects.all(), widget=HiddenInput(), label='', error_messages='')

    class Meta:
        model = Subject
        fields = ('sub_professor', 'sub_name', 'sub_color', 'timetable')

class LessonForm(ModelForm):
    les_location = CharField(widget=TextInput(attrs={'class' : 'input-large', 'id' : 'lesson_location', 'placeholder' : _("location...")}), label='')

    les_day = ChoiceField(choices=( ('Mo', 'Monday'), ('Tu', 'Tuesday'), ('We', 'Wednesday'), ('Th', 'Thursday'), ('Fr', 'Friday'), ('Sa', 'Saturday'), ('Su', 'Sunday')),widget=HiddenInput(attrs={'class':'lesson_day'}), label='')

    les_starttime = TimeField(widget=TextInput(attrs={'class' : 'input-mini', 'id' : 'lesson_starttime', 'type' : 'time', 'placeholder' : 'HH:MM'}), label = _("starttime:"))
    les_endtime = TimeField(widget=TextInput(attrs={'class' : 'input-mini', 'id' : 'lesson_endtime', 'type' : 'time', 'placeholder' : 'HH:MM'}), label = _("endtime:"))

    class Meta:
        model = Lesson
        fields = ('les_location', 'les_starttime', 'les_endtime', 'les_day')
        #How to change Labels here
        #widgets = {
        #    'les_starttime' : TextInput(attrs={'class' : 'input-mini', 'id' : 'lesson_starttime', 'type' : 'time', 'placeholder' : 'HH:MM'}),
        #    'les_endtime' : TextInput(attrs={'class' : 'input-mini', 'id' : 'lesson_endtime', 'type' : 'time', 'placeholder' : 'HH:MM'}),
        #}

class EventForm(ModelForm):

    ev_name = CharField(widget=TextInput(attrs={'class' : 'input-large', 'id' : 'event_name','type' : 'text'}),label='name:')
    ev_description = CharField(widget=TextInput(attrs={'class' : 'input-xxlarge', 'id' : 'event_description','type' : 'text','maxlength':'100'}),label='description:')
    ev_starttime = TimeField(widget=TextInput(attrs={'class' : 'input-medium', 'id' : 'event_starttime', 'type' : 'time', 'placeholder' : 'HH:MM'}),label='starttime:')
    ev_startdate = DateField(widget=SelectDateWidget(attrs={'class' : 'input-medium', 'id' : 'startdate'}),label='startdate:')
    ev_endtime = TimeField(widget=TextInput(attrs={'class' : 'input-medium', 'id' : 'event_endtime', 'type' : 'time', 'placeholder' : 'HH:MM'}),label='endtime:')
    ev_enddate = DateField(widget=SelectDateWidget(attrs={'class' : 'input-medium', 'id' : 'enddate'}),label='enddate:')
    ev_location = CharField(widget=TextInput(attrs={'class' : 'input-large', 'id' : 'event_location','type' : 'text', 'placeholder' : 'street, city'}),label='location:')

    ev_periodic = ChoiceField(choices=(('O', 'Once'),
                                       ('D', 'Daily'),
                                       ('W', 'Weekly'),
                                       ('M', 'Monthly'),
                                       ('Y', 'Yearly')),widget=Select(attrs={'class':'event_periodic'}), label='repeat:')

    ev_color = CharField(widget=TextInput(attrs={'class' : 'input-medium', 'id' : 'event_color', 'type' : 'color', 'value' : '#8e8e8e'}), label='color:')

    class Meta:
        model = Event
        fields = ['ev_name', 'ev_description', 'ev_starttime', 'ev_startdate', 'ev_endtime', 'ev_enddate', 'ev_location', 'ev_periodic', 'ev_color']